package control;

import java.awt.Point;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.util.Enumeration;

import model.quadtree.Type;
import model.quadtree.graph.Node2Graph;
import model.quadtree.graph.QuadTree2Graph;

public class RandomGraph {
	private RandomRect obstacles;
	private Node2Graph root;
	private QuadTree2Graph tree;
	private int[] targetSource = null;
	private static int numberOfGaphs = 10;
	private PrintStream outs;

	public void generateGraph() {
		try {
			outs = new PrintStream(new FileOutputStream(Config.workDir + "test_details.csv"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		outs.println("Id,Obstacles,Edges,Leaf_Nodes");		
		for(int i = 0; i < numberOfGaphs; i++) {
			obstacles = new RandomRect();
			root = new Node2Graph(Type.WHITE, new Point(600, 0), new Point(0, 600));		
			tree = new QuadTree2Graph(root);
			tree.makeChildren(root);
	
			tree.verifyGreys(obstacles.getObstacles());
			tree.verifyNeighborhood();	
	
			writeGraph(tree, i);
			writeStartEndNodes(tree, i);
			
			outs.println(i + "," + obstacles.getObstacles().size() + "," + tree.getEdges().length + "," + tree.getLeafNodes().size());
		}
		outs.close();

	}
	
	private void writeStartEndNodes(QuadTree2Graph tree, int id){
		targetSource = setStartEndNodes(tree);
		//System.out.println("Start: " + targetSource[0] + " End: " + targetSource[1]);		
		try {
			ObjectOutputStream outs = new ObjectOutputStream(new FileOutputStream(Config.workDir + id + "_random_target.bin"));
			outs.writeObject(targetSource);
			outs.close();				
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	private int[] setStartEndNodes(QuadTree2Graph tree) {
		int targetSource[] = new int[2];

		targetSource[0] = setTarget(tree);
		targetSource[1] = setTarget(tree);

		if(targetSource[0] != targetSource[1]){
			return targetSource;
		} else {
			return setStartEndNodes(tree);
		}

	}

	private int setTarget(QuadTree2Graph tree){
		int source = (int)(Math.random() * tree.getLeafNodes().size());

		Enumeration leaf = tree.getLeafNodes().elements();
		Node2Graph current = null;

		for(int i = 0; i <= source; i++)
			current = (Node2Graph)leaf.nextElement();

		if(current.getType() == Type.WHITE) return current.getId();                        
		else return setTarget(tree);    			
	}

	private void writeGraph(QuadTree2Graph tree, int id) {
		double[][] edges = tree.getEdges();		

		try {
			ObjectOutputStream outs = new ObjectOutputStream(new FileOutputStream(Config.workDir + id + "_random_graph.bin"));
			outs.writeObject(edges);
			outs.close();				
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	public int[] getTargetSource() {
		return targetSource;
	}

	public double[][] getEdges() {
		return tree.getEdges();
	}

	public RandomRect getObstacles() {
		return obstacles;
	}

	public QuadTree2Graph getTree() {
		return tree;
	}
	
	public static int getNumberOfGaphs() {
		return numberOfGaphs;
	}

}
